﻿using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.Common;
using BaHuan.Entities;

namespace BaHuan.DataLayer
{
    public class MucChiDAO:BaseDAO
    {

        //Get all MucChi in Database
        public ResultObj GetAllMucChi()
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            List<MucChi> lst = null;
            SqlCommand cmd = CreateSqlCommand("spGetAllMucChi", CommandType.StoredProcedure);
            cmd.Connection.Open();
            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                lst = new List<MucChi>();
                MucChi item = null;
                // reader all data from DB one by one
                while (reader.Read())
                {
                    item = new MucChi();
                    item.Id = Convert.ToInt32(reader["Id"]);
                    item.Ten = Convert.ToString(reader["Ten"]);
                    lst.Add(item);
                }
            }
            result.Result = lst;
            result.ReturnCode = 1;

            return result;
        }


        // save into DB, @id = -1: insert else, update
        public ResultObj SaveMucChi(MucChi mucchi)
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            SqlConnection conObj = DataConnection.GetSqlConnection();
            SqlCommand cmd = CreateSqlCommand("spAddMucChi", CommandType.StoredProcedure);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "spAddMucChi";
            SqlParameter param = new SqlParameter();
            param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, mucchi.Id);
            cmd.Parameters.Add(param);
            param = CreateParameter("@Ten", DbType.String, ParameterDirection.Input, mucchi.Ten);
            cmd.Parameters.Add(param);
            param = CreateParameter("@ReturnVal", DbType.String, ParameterDirection.ReturnValue, result.ReturnCode);
            cmd.Parameters.Add(param);
            conObj.Open();
            cmd.ExecuteNonQuery();
            conObj.Close();

            result.ReturnCode = Convert.ToInt32(cmd.Parameters["@ReturnVal"].Value);
            return result;
        }


        // Get all MucChi in DB with condition
        public ResultObj GetAllMucChiByID(MucChi mucchi)
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            List<MucChi> lst = null;
            SqlCommand cmd = CreateSqlCommand("spGetAllMucChiByID", CommandType.StoredProcedure);
            SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, mucchi.Id);
            cmd.Parameters.Add(param);
            cmd.Connection.Open();
            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                lst = new List<MucChi>();
                MucChi item = null;
                // reader all data from DB one by one
                while (reader.Read())
                {
                    item = new MucChi();
                    item.Id = Convert.ToInt32(reader["Id"]);
                    item.Ten = Convert.ToString(reader["Ten"]);
                    lst.Add(item);
                }
            }
            result.Result = lst;
            result.ReturnCode = 1;

            return result;
        }


        // Delete a MucChi out of DB with condition
        public ResultObj DeleteMucChi(MucChi mucchi)
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            SqlConnection conObj = DataConnection.GetSqlConnection();
            SqlCommand cmd = CreateSqlCommand("spDeleteMucChi", CommandType.StoredProcedure);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "spDeleteMucChi";
            SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, mucchi.Id);
            cmd.Parameters.Add(param);
            conObj.Open();
            result.ReturnCode = cmd.ExecuteNonQuery();
            conObj.Close();

            return result;
        }

    }
}
